package main

import (
	"fmt"
	"sort"
)

type intSlice []int

func main() {
	arr := [4]int{1, 2, 3, 4}
	fmt.Println(arr)
	slice := []int{12, 5, 61, 44, 6, 7, 122}
	bubbleSort(slice)
	fmt.Println(slice)
	slice1 := intSlice{54, 12, 531, 452, 61, 17, 1122}
	sort.Sort(slice1)
	fmt.Println(slice1)
	slice2 := slice1[1:5]
	fmt.Println(slice2)
	str := "abdeft"
	rs := []rune(str)
	fmt.Println(len(rs))
	fmt.Println(subStr(str, 1, -1))
}

func subStr(str string, start int, length int) string {
	rs := []rune(str)
	var strLen = len(rs)
	//fmt.Println(strLen)
	var end int
	if length > 0 {
		end = start + length
	} else {
		end = strLen + length
	}
	sub := rs[start:end]
	return string(sub)
}

func bubbleSort(slice intSlice) {
	count := len(slice)
	for i := 0; i < count; i++ {
		for j := i + 1; j < count; j++ {
			if slice[i] > slice[j] {
				slice[i], slice[j] = slice[j], slice[i]
			}
		}
	}
}

func (slice intSlice) Len() int {
	return len(slice)
}

func (slice intSlice) Less(i, j int) bool {
	return slice[i] > slice[j] //从大到小
}

func (slice intSlice) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}
